<?php

global $_GPC;
global $_W;
$shopid = check_right(2);
if (!$shopid) {
    message('请先选择需要管理的店铺', $this->createWebUrl('shop'), 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=\'' . $shopid . '\'');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
if ($operation == 'supplier') {
    $where = '';
    if ($_GPC['keyword']) {
        $where .= ' and (realname like \'%' . $_GPC['keyword'] . '%\' or mobile like \'%' . $_GPC['keyword'] . '%\' )';
    }
    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_supplier') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' ' . $where . ' order by id desc');
} else {
    if ($operation == 'getsupplier') {
        $id = intval($_GPC['id']);
        if ($id != 0) {
            $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_supplier') . ' WHERE id = :id ', array(':id' => $id));
            exit(json_encode(array('success' => true, 'item' => $item)));
        }
        exit(json_encode(array('success' => true, 'item' => '')));
    } else {
        if ($operation == 'postsupplier') {
            $id = intval($_GPC['id']);
            $data = array('weid' => $_W['uniacid'], 'title' => $_GPC['title'], 'groupid' => intval($shopid), 'realname' => $_GPC['realname'], 'mobile' => $_GPC['mobile'], 'createtime' => TIMESTAMP);
            if (!empty($id)) {
                unset($data['createtime']);
                pdo_update('j_hmoney_supplier', $data, array('id' => $id));
            } else {
                pdo_insert('j_hmoney_supplier', $data);
            }
            exit(json_encode(array('success' => true, 'item' => $item)));
        } else {
            if ($operation == 'deletesupplier') {
                $id = intval($_GPC['id']);
                if ($id) {
                    pdo_delete('j_hmoney_supplier', array('id' => $id));
                }
                message('删除成功', $this->createWebUrl('store', array('op' => 'supplier')), 'success');
            } else {
                if ($operation == 'display') {
                    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_store') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' order by id desc');
                    $supplierlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_supplier') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' ORDER BY id asc');
                    $supplierary = array();
                    foreach ($supplierlist as $row) {
                        $supplierary[$row['id']] = $row['title'];
                    }
                    $grouplist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' ORDER BY id asc');
                    $groupary = array();
                    foreach ($grouplist as $row) {
                        $groupary[$row['id']] = $row['companyname'];
                    }
                    $goodcategory = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goodcategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=:a ORDER BY displayorder asc,id desc', array(':a' => $shopid));
                    $goodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and pcate=:a ORDER BY displayorder asc,id desc', array(':a' => $goodcategory[0]['id']));
                } else {
                    if ($operation == 'searchgoods') {
                        $keyword = trim($_GPC['keyword']);
                        if ($keyword) {
                            $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE  groupid =\'' . $shopid . '\' and goodsn=:a order by id desc limit 1', array(':a' => $keyword));
                            if (!$item) {
                                exit(json_encode(array('success' => false)));
                            }
                            exit(json_encode(array('success' => true, 'item' => $item)));
                        }
                        exit(json_encode(array('success' => false)));
                    } else {
                        if ($operation == 'getstoreno') {
                            $ordersn = $_GPC['ordersn'];
                            $order = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_store') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and ordersn=:a', array(':a' => $ordersn));
                            if (!$order) {
                                exit(json_encode(array('success' => false, 'msg' => '单号不存在')));
                            }
                            $goodlist = pdo_fetchall('SELECT a.*,b.* FROM ' . tablename('j_hmoney_storedetail') . ' as a left join(select * from ' . tablename('j_hmoney_goods') . ') as b on a.goodid=b.id WHERE a.ordersn=\'' . $ordersn . '\' order by a.id asc');
                            exit(json_encode(array('success' => true, 'item' => $order, 'goodlist' => $goodlist)));
                        } else {
                            if ($operation == 'poststore') {
                                $ordersn = $_GPC['ordersn'];
                                $data = array('weid' => $_W['uniacid'], 'groupid' => intval($shopid), 'createtime' => TIMESTAMP, 'uid' => intval($uid), 'sid' => intval($_GPC['sid']), 'totalfee' => floatval($_GPC['totalfee']), 'paidfee' => floatval($_GPC['paidfee']), 'otherfee' => floatval($_GPC['otherfee']), 'paytype' => intval($_GPC['paytype']), 'remark' => trim($_GPC['remark']), 'status' => 0);
                                if (!$ordersn) {
                                    $data['ordersn'] = 'PUR' . TIMESTAMP;
                                    pdo_insert('j_hmoney_store', $data);
                                } else {
                                    unset($data['createtime']);
                                    pdo_update('j_hmoney_store', $data, array('ordersn' => $ordersn));
                                    pdo_delete('j_hmoney_store', array('ordersn' => $ordersn));
                                }
                                $sql = 'insert into ' . tablename('j_hmoney_storedetail') . ' (weid,groupid,ordersn,goodid,num,price,totalfee,paidfee,status,createtime)VALUES';
                                $ary = array();
                                $num = 0;
                                foreach ($_GPC['num'] as $index => $row) {
                                    $temp = array(0 => $_W['uniacid'], 1 => $shopid, 2 => $data['ordersn'], 3 => $index, 4 => $row, 5 => $_GPC['price'][$index], 6 => $_GPC['total'][$index], 7 => $_GPC['paid'][$index], 8 => 0, 9 => TIMESTAMP);
                                    $ary[] = '(\'' . implode('\',\'', $temp) . '\')';
                                    $num += $row;
                                }
                                $sql = $sql . implode(',', $ary);
                                pdo_run($sql);
                                pdo_update('j_hmoney_store', array('num' => $num), array('ordersn' => $data['ordersn']));
                                exit(json_encode(array('success' => true)));
                            } else {
                                if ($operation == 'deletestore') {
                                    $id = intval($_GPC['id']);
                                    if ($id) {
                                        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_store') . ' WHERE id = :id ', array(':id' => $id));
                                        if ($item['status'] > 0) {
                                            message('已入库订单不能删除哦', $this->createWebUrl('store', array('op' => 'display')), 'error');
                                        }
                                        $ordersn = $item['ordersn'];
                                        pdo_delete('j_hmoney_store', array('id' => $id));
                                        pdo_delete('j_hmoney_storedetail', array('ordersn' => $ordersn));
                                    }
                                    message('删除成功', $this->createWebUrl('store', array('op' => 'display')), 'success');
                                } else {
                                    if ($operation == 'instore') {
                                        $id = intval($_GPC['id']);
                                        if (!$id) {
                                            exit(json_encode(array('success' => false, 'msg' => '订单编号不能为空')));
                                        }
                                        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_store') . ' WHERE id = :id ', array(':id' => $id));
                                        if ($item['status'] > 0) {
                                            exit(json_encode(array('success' => false, 'msg' => '已入库订单不能重复入库')));
                                        }
                                        $ordersn = $item['ordersn'];
                                        $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_storedetail') . ' WHERE ordersn = :a ', array(':a' => $ordersn));
                                        $ary = array();
                                        foreach ($list as $row) {
                                            $ary[] = 'update ' . tablename('j_hmoney_goods') . ' set store=store+' . $row['num'] . ' where id=' . $row['goodid'];
                                        }
                                        $sql = implode(';', $ary);
                                        pdo_run($sql);
                                        pdo_update('j_hmoney_store', array('status' => 1, 'instoretime' => TIMESTAMP), array('id' => $id));
                                        pdo_update('j_hmoney_storedetail', array('status' => 1), array('ordersn' => $ordersn));
                                        exit(json_encode(array('success' => true)));
                                    } else {
                                        if ($operation == 'getpcategood') {
                                            $pcate = intval($_GPC['pcate']);
                                            if (!$pcate) {
                                                exit(json_encode(array('success' => false)));
                                            }
                                            $goodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and pcate=:a ORDER BY displayorder asc,id desc', array(':a' => $pcate));
                                            exit(json_encode(array('success' => true, 'list' => $goodlist)));
                                        } else {
                                            if ($operation == 'postsalereturn') {
                                                $ordersn = trim($_GPC['ordersn']);
                                                $order = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_store') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and ordersn=:a ', array(':a' => $ordersn));
                                                if ($order['status'] == 2) {
                                                    exit(json_encode(array('success' => false, 'msg' => '订单已全部退货')));
                                                }
                                                $outordersn = 'OUT' . TIMESTAMP;
                                                $sql = 'insert into ' . tablename('j_hmoney_storereturndetail') . ' (weid,groupid,outordersn,ordersn,goodid,num,price,totalfee,paidfee,status,createtime)VALUES';
                                                $ary = array();
                                                $num = 0;
                                                $ary2 = array();
                                                $sql2 = 'update ' . tablename('j_hmoney_storedetail') . ' SET `num` = CASE goodid ';
                                                $idary = array();
                                                foreach ($_GPC['num'] as $index => $row) {
                                                    if ($row != 0) {
                                                        $this->_2money($_GPC['price'][$index] * $row * 100);
                                                        $temp = array(0 => $_W['uniacid'], 1 => $shopid, 2 => $outordersn, 3 => $ordersn, 4 => $index, 5 => $row, 6 => $_GPC['price'][$index], 7 => $this->_2money($_GPC['price'][$index] * $row * 100), 8 => $_GPC['paid'][$index], 9 => 1, 10 => TIMESTAMP);
                                                        $ary[] = '(\'' . implode('\',\'', $temp) . '\')';
                                                        $sql2 .= ' WHEN ' . $index . ' THEN `num`-' . $row . ' ';
                                                        $idary[] = $index;
                                                        $num += $row;
                                                    }
                                                }
                                                $sql2 .= ' END ';
                                                $sql2 .= ' WHERE goodid IN (' . implode(',', $idary) . ') and ordersn=\'' . $ordersn . '\'';
                                                if (count($ary) == 0) {
                                                    exit(json_encode(array('success' => false, 'msg' => '请选择退货商品')));
                                                }
                                                $data = array('weid' => $_W['uniacid'], 'groupid' => intval($shopid), 'createtime' => TIMESTAMP, 'uid' => intval($uid), 'num' => intval($num), 'totalfee' => floatval($_GPC['totalfee']), 'paidfee' => floatval($_GPC['paidfee']), 'otherfee' => floatval($_GPC['otherfee']), 'paytype' => intval($_GPC['paytype']), 'remark' => trim($_GPC['remark']), 'ordersn' => trim($ordersn), 'outordersn' => trim($outordersn), 'status' => 1);
                                                pdo_insert('j_hmoney_storereturn', $data);
                                                $sql = $sql . implode(',', $ary);
                                                pdo_run($sql);
                                                pdo_run($sql2);
                                                $num = pdo_fetchcolumn('SELECT sum(num) FROM ' . tablename('j_hmoney_storedetail') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and ordersn=:a ', array(':a' => $ordersn));
                                                if ($num == 0) {
                                                    pdo_update('j_hmoney_store', array('status' => 2, 'num' => 0), array('ordersn' => $ordersn));
                                                } else {
                                                    pdo_update('j_hmoney_store', array('num' => $num), array('ordersn' => $ordersn));
                                                }
                                                if ($order['status'] == 1) {
                                                    $sql2 = 'update ' . tablename('j_hmoney_goods') . ' SET `store` = CASE id ';
                                                    $idary = array();
                                                    foreach ($_GPC['num'] as $index => $row) {
                                                    }
                                                    $sql2 .= ' END ';
                                                    $sql2 .= ' WHERE id IN (' . implode(',', $idary) . ') ';
                                                    pdo_run($sql2);
                                                }
                                                exit(json_encode(array('success' => true)));
                                            } else {
                                                if ($operation == 'storeview') {
                                                    $ordersn = $_GPC['ordersn'];
                                                    $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_store') . ' WHERE ordersn=:a ', array(':a' => $ordersn));
                                                    $goodlist = pdo_fetchall('SELECT a.*,b.* FROM ' . tablename('j_hmoney_storedetail') . ' as a left join(select * from ' . tablename('j_hmoney_goods') . ') as b on a.goodid=b.id WHERE a.ordersn=\'' . $ordersn . '\' order by a.id asc');
                                                    $goodary = array();
                                                    foreach ($goodlist as $row) {
                                                        $goodary[$row['goodid']] = $row;
                                                    }
                                                    $salereturn = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_storereturn') . ' WHERE ordersn=:a order by id asc', array(':a' => $ordersn));
                                                    $salegoodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_storereturndetail') . ' WHERE ordersn=:a order by id asc', array(':a' => $ordersn));
                                                    $salegoodary = array();
                                                    foreach ($salegoodlist as $row) {
                                                        if (!isset($salegoodary[$row['outordersn']])) {
                                                            $salegoodary[$row['outordersn']] = array();
                                                        }
                                                        $salegoodary[$row['outordersn']][] = $row;
                                                    }
                                                    include $this->template('web/store/store_tpl');
                                                    exit(0);
                                                } else {
                                                    if ($operation != 'checkstock') {
                                                        if ($operation == 'getgoodstore') {
                                                            if (!$_GPC['num']) {
                                                                exit(json_encode(array('success' => false, 'msg' => '请先录入商品.')));
                                                            }
                                                            $keys = array_keys($_GPC['num']);
                                                            if (!count($keys)) {
                                                                exit(json_encode(array('success' => false, 'msg' => '请先录入商品')));
                                                            }
                                                            $list = pdo_fetchall('SELECT id,store FROM ' . tablename('j_hmoney_goods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id in(' . implode(',', $keys) . ') order by id desc');
                                                            exit(json_encode(array('success' => true, 'list' => $list)));
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
include $this->template('web/' . $cfg['floder'] . '/store');